package repository

import (
	"go-web/config"
	"go-web/model"
	"gorm.io/gorm"
	"time"
)

func SaveShortLink(link *model.ShortLink) error {
	return config.DB.Create(link).Error
}

func GetOriginalURLByShortID(shortLink string) (model.ShortLink, error) {

	var link model.ShortLink
	err := config.DB.Where("short_link = ?", shortLink).First(&link).Error

	return link, err
}
func IncrementClick(shortID string) error {
	return config.DB.Model(&model.ShortLink{}).
		Where("short_link = ?", shortID).
		Updates(map[string]interface{}{
			"clicks":        gorm.Expr("clicks + 1"),
			"last_click_at": time.Now(),
		}).Error
}
